//
//  MeterAPI.swift
//  Employee
//
//  Created by tancheng on 2018/6/21.
//  Copyright © 2018年 remotevillage. All rights reserved.
//

import Foundation
import Moya

let MeterProvider = MoyaProvider<MeterAPI>(
    manager: DefaultAlamofireManager.sharedManager,
    plugins:[
        //NetworkLoggerPlugin(verbose: false)
    ]
)

enum MeterAPI {
    /// 添加仪表
    case addMeter(send: AddMeterSend)
    /// 移动抄表列表
    case getReadingMeterTaskList(send: ReadingMeterTaskSend)
    /// 移动抄表详情
    case getReadingMeterTaskDetail(send: ReadingMeterDetailSend)
    /// 移动抄表详情提交
    case submitReadingMeterTaskDetail(send: ReadingMeterDetailSubmitSend)
    
}

extension MeterAPI: TargetType {
    
    /// The target's base `URL`.
    public var baseURL: URL {
        return URL(string: GlobalConfig.BASE_OUT_URL)!
    }
    
    /// The path to be appended to `baseURL` to form the full `URL`.
    public var path: String {
        switch self {
        case .addMeter(send:):
            return "/ms/meter/add.json"
        case .getReadingMeterTaskList(send:):
            return "/ms/meter/todo.json"
        case .getReadingMeterTaskDetail(send:):
            return "/ms/meter/read/detail.json"
        case .submitReadingMeterTaskDetail(send:):
            return "/ms/meter/read/add.json"
        }
        
    }
    
    var parameters: [String : Any]? {
       
        switch self {
        case  let .addMeter(send):
            return  [
                "access_token": Token.shared.token,
                "ascription":send.ascription,
                "houseId":send.houseId,
                "type":send.type,
                "meterNumber":send.meterNumber,
                "firstRead":send.firstRead,
                "regionId":send.regionId,
                "remark":send.remark,
                "imageIds":send.imageIds,
            ]
            
        case let .getReadingMeterTaskList(send):
            var params = [
                "access_token": Token.shared.token,
                "page.size":send.pageSize,
                "page.pn":send.pageIndex,
                "type":"\(send.type)"
                ]
            
            
            if !send.meterReadDate.isEmpty {
                params["meterReadDate"] = send.meterReadDate
            }
            
            if send.keyword != nil {
                params["keyWord"] = send.keyword!
            }
            return params
           
        case  let .getReadingMeterTaskDetail(send):
            
            if send.meterId != nil && send.taskId.isEmpty {
                return [//扫描二维码，返meterId，taskId，当taskId为空时穿meterId
                    "access_token": Token.shared.token,
                    "meterId":send.meterId!,
                ]
            }
            //
            return  [
                "access_token": Token.shared.token,
                "taskId":send.taskId,
            ]
            
        case  let .submitReadingMeterTaskDetail(send):
            return  [
                "access_token": Token.shared.token,
                "taskId":send.taskId,
                "fileIds":send.fileIds,
                "meterRead":send.meterRead
            ]
            
            
        }
       
    }
    
    
    /// The HTTP method used in the request.
    public var method: Moya.Method {
        
        switch self {
        case .addMeter(send:), .submitReadingMeterTaskDetail(send:):
            return .post
        default: return .get
        }
    }
    
    /// Provides stub data for use in testing.
    public var sampleData: Data { return Data() }
    
    /// The type of HTTP task to be performed.
    public var task: Task {
        return  .requestParameters(parameters: self.parameters?.signParame(GlobalConfig.privateKey) ?? [:], encoding: URLEncoding.default)
    }
    
    /// The type of validation to perform on the request. Default is `.none`.
   // var validationType: ValidationType { get }
    
    /// The headers to be used in the request.
    public var headers: [String: String]? {
        return GlobalConfig.httpHeaders
    }
    
    
}
